<template>
    <div>
        <div class="operarea" v-if="isHowPage == 0">
            <div class="seach">
                <el-input v-model="seachForm.name" :placeholder="$t('name')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="getData"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="showAddDlog">{{$t('add')}}</el-button>
            </div>
        </div>
        <div class="operarea" v-if="isHowPage == 1">
            <div class="seach">
                <el-input v-model="getSizeForm.cloSize" :placeholder="$t('clolength')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="seachSizeData"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="addSizeDialogVisible = true">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-tabs v-model="seachForm.type" type="card" @tab-click="saveActive">
            <el-tab-pane :label="$t('model')" name="robePattern">
                <el-card class="cardBox">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item>大袍</el-breadcrumb-item>
                        <el-breadcrumb-item>版型</el-breadcrumb-item>
                    </el-breadcrumb>
                    <el-table style="width: 100%" :data="modelTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                        <el-table-column prop="name" :label="$t('name')">
                        </el-table-column>
                        <el-table-column prop="bh" :label="$t('serialnumber')">
                        </el-table-column>
                        <el-table-column :label="$t('image')">
                            <template v-slot="scope">
                                <el-image v-if="scope.row.imgUrl !== '' && scope.row.imgUrl !== null" :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" width="150" :label="$t('operation')">
                            <template v-slot="scope">
                                <el-button @click.stop="showModifyModel(scope.row)" size="mini">编辑</el-button>
                                <el-button @click.stop="showDelModel(scope.row.uuid)" size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                        small
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :pager-count="11"
                        :current-page.sync="seachForm.pageNo"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="seachForm.pageSize"
                        layout="sizes, prev, pager, next"
                        :total="total">
                    </el-pagination>
                </el-card>
            </el-tab-pane>
            <el-tab-pane :label="$t('size')" name="size">
                <el-card class="cardBox">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item>大袍</el-breadcrumb-item>
                        <el-breadcrumb-item>尺码</el-breadcrumb-item>
                    </el-breadcrumb>
                    <el-table style="width: 100%" :data="sizeTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                        <el-table-column prop="cloSize" :label="$t('clolength')">
                        </el-table-column>
                        <el-table-column prop="usagee" :label="$t('dosage')">
                        </el-table-column>
                        <el-table-column prop="address" width="150" :label="$t('operation')">
                            <template v-slot="scope">
                                <el-button @click.stop="showModifySize(scope.row)" size="mini">编辑</el-button>
                                <el-button @click.stop="showDelSize(scope.row.uuid)" size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                        small
                        @size-change="handleSizeListChange"
                        @current-change="handleListCurrentChange"
                        :pager-count="11"
                        :current-page.sync="getSizeForm.pageNo"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="getSizeForm.pageSize"
                        layout="sizes, prev, pager, next"
                        :total="sizeTotal">
                    </el-pagination>
                </el-card>
            </el-tab-pane>
        </el-tabs>
        <!-- 版型 -->
        <el-dialog :title="$t('add')" :visible.sync="addModelDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addModelDlogClose(addModelDataForm)">
            <el-form :model="addModelDataForm" :rules="addModelRules" :ref="addModelDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="addModelDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('serialnumber')" label-width="70px" prop="bh">
                    <el-input v-model="addModelDataForm.bh" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="70px" prop="imgUrl">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="hadleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addModelDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addModelData(addModelDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyModelDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyModelDlogClose(modifyModelDataForm)">
            <el-form :model="modifyModelDataForm" :rules="modifyModelRules" :ref="modifyModelDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="modifyModelDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('serialnumber')" label-width="70px" prop="bh">
                    <el-input v-model="modifyModelDataForm.bh" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="70px" prop="imgUrl">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="hadleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyModelDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyModelData(modifyModelDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除 -->
        <el-dialog :title="$t('hint')" :visible.sync="delModelDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delModelDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delModelData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 尺寸 -->
        <el-dialog :title="$t('add')" :visible.sync="addSizeDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addSizeDlogClose(addSizeDataForm)">
            <el-form :model="addSizeDataForm" :rules="addSizeRules" :ref="addSizeDataForm" class="motuoForm">
                <el-form-item :label="$t('clolength')" label-width="70px" prop="cloSize">
                    <el-input v-model="addSizeDataForm.cloSize" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('dosage')" label-width="70px" prop="usagee">
                    <el-input v-model="addSizeDataForm.usagee" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addSizeDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addSizeData(addSizeDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <el-dialog :title="$t('modify')" :visible.sync="modifySizeDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifySizeDlogClose(modifySizeDataForm)">
            <el-form :model="modifySizeDataForm" :rules="modifySizeRules" :ref="modifySizeDataForm" class="motuoForm">
                <el-form-item :label="$t('clolength')" label-width="70px" prop="cloSize">
                    <el-input v-model="modifySizeDataForm.cloSize" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('dosage')" label-width="70px" prop="usagee">
                    <el-input v-model="modifySizeDataForm.usagee" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifySizeDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifySizeData(modifySizeDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <el-dialog :title="$t('hint')" :visible.sync="delSizeDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delSizeDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delSizeData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    getRobesElementAll,
    addRobesElementItem,
    editRobesElementItem,
    deleteRobesElementItem,
    getRobesSize,
    addRobesSize,
    updateRobesSize,
    deleteRobesSize
} from "@/libs/reqlist.js"
export default {
    data () {
        return {
            activeName: '',
            total: 0,
            seachForm: {
                deleteStatus: 0,
                pageNo: 1,
                pageSize: 10,
                name: '',
                bh: '',
                type: 'robePattern',
                cloType: '0'
            },
            imgList: [],
            // 判断是哪个页面
            isHowPage: 0,
            // 版型
            doType: 0,
            modelTab: [],
            addModelDialogVisible: false,
            addModelDataForm: {
                name: '',
                bh: '',
                type: '',
                cloType: '0',
                imgUrl: ''
            },
            addModelRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                bh: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            modifyModelDialogVisible: false,
            modifyModelDataForm: {
                name: '',
                bh: '',
                type: '',
                cloType: '0',
                imgUrl: '',
                uuid: ''
            },
            fileList: [],
            modifyModelRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                bh: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            delModelDialogVisible: false,
            deleteId: '',
            // 尺码
            getSizeForm: {
                pageNo: 1,
                pageSize: 10,
                cloSize: ''
            },
            sizeTotal: 0,
            sizeTab: [],
            addSizeDialogVisible: false,
            addSizeDataForm: {
                cloSize: '',
                usagee: ''
            },
            addSizeRules: {
                cloSize: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                usagee: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            modifySizeDialogVisible: false,
            modifySizeDataForm: {
                uuid: '',
                cloSize: '',	
                usagee: ''	
            },
            modifySizeRules: {
                cloSize: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                usagee: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            delSizeDialogVisible: false,
            delSizeUuid: ''
        }
    },
    methods: {
        saveActive(tab) {
            this.seachForm.type = tab.name
            this.activeName = tab.name
            window.sessionStorage.setItem('robeAct', this.activeName)
            switch (this.activeName) {
                case 'robePattern':
                    this.isHowPage = 0
                    this.seachData()
                    break
                case 'size':
                    this.isHowPage = 1
                    this.getSizeData()
                    break
            }
        },
        seachData () {
            this.imgList = []
            getRobesElementAll(this.seachForm).then(res => {
                if (res.data.code === 0) {
                    res.data.data.data.forEach(item => {
                        if (item.imgUrl !== '' && item.imgUrl !== null) this.imgList.push(item.imgUrl)
                    })
                    this.modelTab = res.data.data.data
                    this.total = res.data.data.totalPage
                }
            })
        },
        // 搜索
        getData() {
            this.seachForm.pageNo = 1
            this.seachData()
        },
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        getSrcList(index) {
            return this.imgList.slice(index).concat(this.imgList.slice(0, index))
        },
        // 上传文件超出个数触发
        scotland() {
            this.$message.warning('只能选择一个文件')
        },
        handleImg(res) {
            if (res.code == 0) {
                switch (this.doType) {
                    case 1:
                        this.addModelDataForm.imgUrl = res.data
                        break
                    case 2:
                        this.modifyModelDataForm.imgUrl = res.data
                        break
                }
            }
        },
        hadleRemove(file) {
            if (file) {
                switch (this.doType) {
                    case 1:
                        this.fileList = []
                        this.addModelDataForm.imgUrl = ''
                        break
                    case 2:
                        this.fileList = []
                        this.modifyModelDataForm.imgUrl = ''
                        break
                }
            }
        },
        // 领型
        showAddDlog() {
            this.doType = 1
           this.addModelDialogVisible = true
        },
        addModelDlogClose (form) {
            this.fileList = []
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        addModelData (form) {
            this.addModelDataForm.type = this.seachForm.type
            this.$refs[form].validate(valid => {
                if (!valid) return
                if (this.addModelDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                addRobesElementItem(this.addModelDataForm).then(res => {
                    if (res.data.code === 0) {
                        this.addModelDialogVisible = false
                        this.seachData()
                        this.$message.success(this.$t('sucadd'))
                    }
                })
            })
        },
        showModifyModel (data) {
            this.doType = 2
            this.modifyModelDataForm.name = data.name
            this.modifyModelDataForm.bh = data.bh
            this.modifyModelDataForm.type = data.type
            this.modifyModelDataForm.cloType = data.cloType
            this.modifyModelDataForm.imgUrl = data.imgUrl
            if (data.imgUrl !== '' && data.imgUrl !== null) this.fileList.push({ name: data.name, url: data.imgUrl })
            this.modifyModelDataForm.uuid = data.uuid
            this.modifyModelDialogVisible = true
        },
        modifyModelDlogClose (form) {
            this.fileList = []
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        modifyModelData (form) {
            this.modifyModelDataForm.type = this.seachForm.type
            this.$refs[form].validate(valid => {
                if (!valid) return
                if (this.modifyModelDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                editRobesElementItem(this.modifyModelDataForm).then(res => {
                    if (res.data.code === 0) {
                        this.modifyModelDialogVisible = false
                        this.$message({
                            type: 'success',
                            message: this.$t('modifysuc')
                        })
                        this.seachData()
                    }
                })
            })
        },
        showDelModel (uuid) {
            this.deleteId = uuid
            this.delModelDialogVisible = true
        },
        delModelData () {
            deleteRobesElementItem({ uuid: this.deleteId }).then(res => {
                if (res.data.code === 0) {
                   this.$message({
                        type: 'success',
                        message: this.$t('delsuc')
                    })
                    this.delModelDialogVisible = false
                    this.seachData()
                }
            })
            
        },
        // 尺寸
        getSizeData () {
            getRobesSize(JSON.stringify(this.getSizeForm)).then(res => {
                if (res.data.code == 0) {
                    this.sizeTab = res.data.data.data
                    this.sizeTotal = res.data.data.hits
                }
            })
        },
        seachSizeData() {
            this.getSizeForm.pageNo = 1
            this.getSizeData()
        },
        handleSizeListChange (newSize) {
            this.getSizeForm.pageSize = newSize
            this.getSizeData()
        },
        handleListCurrentChange (newNum) {
            this.getSizeForm.pageNo = newNum
            this.getSizeData()
        },
        addSizeDlogClose (form) {
            this.$refs[form].resetFields()
        },
        addSizeData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                addRobesSize(JSON.stringify(this.addSizeDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.addSizeDialogVisible = false
                        this.getSizeData()
                        this.$message.success(this.$t('sucadd'))
                    }
                })
            })
        },
        showModifySize (data) {
            this.modifySizeDataForm.uuid = data.uuid
            this.modifySizeDataForm.cloSize = data.cloSize
            this.modifySizeDataForm.usagee = data.usagee
            this.modifySizeDialogVisible = true
        },
        modifySizeDlogClose (form) {
            this.$refs[form].resetFields()
        },
        modifySizeData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                updateRobesSize(JSON.stringify(this.modifySizeDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.modifySizeDialogVisible = false
                        this.getSizeData()
                        this.$message.success(this.$t('modifysuc'))
                    }
                })
            })
        },
        showDelSize (uuid) {
            this.delSizeUuid = uuid
            this.delSizeDialogVisible = true
        },
        delSizeData () {
            deleteRobesSize(JSON.stringify({ uuid: this.delSizeUuid })).then(res => {
                if (res.data.code == 0) {
                    this.delSizeDialogVisible = false
                    this.getSizeData()
                    this.$message.success(this.$t('delsuc'))
                }
            })
        }
    },
    mounted () {
        this.seachData()
    }
}
</script>

<style lang="less" scoped>
@import './robes.less';
</style>
